<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter :title="getTitle" :width="700" :maskClosable="false" @ok="handleSubmit">
    <a-card title="卡基础信息" :bordered="false">
      <BasicForm @register="registerCommonForm"></BasicForm>
    </a-card>
    <a-card title="会员卡设置" :bordered="false">
      <BasicForm @register="registerSetForm"></BasicForm>
    </a-card>
    <a-card title="卡号规则设置" :bordered="false">
      <BasicForm @register="registerVipcardNoForm"></BasicForm>
    </a-card>
    <a-card title="协议设置" :bordered="false">
      <BasicForm @register="registerAgreementForm"></BasicForm>
    </a-card>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { computed, ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { agreementFormSchema, vipcardCommonFormSchema, vipcardNoFormSchema, vipcardSetFormSchema } from '../data/data';
import { add, get, update } from '../data/api';

const isUpdate = ref(true);
const emit = defineEmits(['register', 'success']);
//计算标题
const getTitle = computed(() => (!isUpdate.value ? '新增卡种' : '编辑卡种'));

const [registerCommonForm, { resetFields: resetCommonFields, setFieldsValue: setCommonFields, validate: validateCommon }] = useForm({
  labelWidth: 100,
  labelCol: { span: 4 },
  schemas: vipcardCommonFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

const [registerSetForm, { resetFields: resetSetFields, setFieldsValue: setSetFields, validate: validateSet }] = useForm({
  labelWidth: 100,
  labelCol: { span: 4 },
  schemas: vipcardSetFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

const [registerVipcardNoForm, { resetFields: resetVipcardNoFields, setFieldsValue: setVipcardNoFields, validate: validateVipcardNo }] = useForm({
  labelWidth: 100,
  labelCol: { span: 4 },
  schemas: vipcardNoFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

const [registerAgreementForm, { resetFields: resetAgreementFields, setFieldsValue: setAgreementFields, validate: validateAgreement }] = useForm({
  labelWidth: 100,
  labelCol: { span: 4 },
  schemas: agreementFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  setDrawerProps({ confirmLoading: true });
  await resetCommonFields();
  await resetSetFields();
  await resetVipcardNoFields();
  await resetAgreementFields();
  setDrawerProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    const vipcardId = data.record.vipcardId;
    let vipCardDetail = await get({ vipcardId: vipcardId, handlerName: 'busVipCardGetHandler' });
    setCommonFields(vipCardDetail);
    setSetFields(vipCardDetail);
    setVipcardNoFields(vipCardDetail);
    setAgreementFields(vipCardDetail);
  }
});

/**
 * 表单提交
 */
async function handleSubmit() {
  try {
    setDrawerProps({ confirmLoading: true });
    const [commonValues, setValues, vipcardNoValues, agreementValues] = await Promise.all([validateCommon(), validateSet(), validateVipcardNo(), validateAgreement()]);
    const allValues = Object.assign({}, commonValues, setValues, vipcardNoValues, agreementValues);
    console.log(allValues);
    setDrawerProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await update(allValues);
    } else {
      await add(allValues);
    }
    closeDrawer();
    emit('success', false);
    closeDrawer();
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
